package com.wanmait.wisdom.manage.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.wisdom.entity.Resident;
import com.wanmait.wisdom.manage.service.ManageResidentService;
import com.wanmait.wisdom.util.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("manage")
public class ManageResidentController {

    @Resource
    private ManageResidentService manageResidentService;

    @GetMapping("getResidentCount")
    public AjaxResult getResidentCount(){
        return AjaxResult.success(manageResidentService.getResidentCount());
    }

    @PostMapping("insertResident")
    public AjaxResult addResident(@RequestBody Resident resident ,Integer houseId){
        if (resident.getId() == null){
            if (resident.getCard().length()>6){
                resident.setPassword(resident.getCard().substring(resident.getCard().length() - 6));
                manageResidentService.addResident(resident);
                return AjaxResult.success("添加成功");
            }
            else{
                return AjaxResult.error(9999,"身份证号格式不正确");
            }
        }else{
            resident.setPassword(resident.getCard().substring(resident.getCard().length() - 6));
            manageResidentService.updateResident(resident, houseId);
            return AjaxResult.success("修改成功");
        }
    }

    @GetMapping("checkParking")
    public AjaxResult checkParking(Integer id){
        Integer count = manageResidentService.getCountByParkingId(id);
        return AjaxResult.success().put("count",count);
    }

    @PostMapping("findAllResident")
    public AjaxResult findAllResident(@RequestBody Resident searchResident, Integer pageNum, Date startTime, Date endTime){
        System.out.println(searchResident.getUnitId()+"----"+searchResident.getBuildingId()+"---"+searchResident.getHouseId()+"----"+searchResident.getResidentTypeId());
        if (pageNum==null){
            pageNum=1;
        }
        PageInfo<Resident> pageInfo = manageResidentService.findAllResident(searchResident,pageNum,startTime,endTime,7);
        return AjaxResult.success().put("pageInfo",pageInfo);
    }

    @GetMapping("getResidentById")
    public AjaxResult getResidentById(Integer id){
        Resident resident = manageResidentService.findResidentById(id);
        return AjaxResult.success().put("resident",resident);
    }

    @GetMapping("checkPersonSize")
    public AjaxResult checkPersonSize(Integer id){
        Integer count=manageResidentService.checkPersonSize(id);
            return  AjaxResult.success().put("count",count);
    }
    //删除居民
    @GetMapping("deleteResident")
    public AjaxResult deleteResident(Integer id,Integer houseId){
        manageResidentService.deleteResident(id,houseId);
        return AjaxResult.success("删除成功");
    }
    //验证身份证正面
//    @PostMapping("uploadCardPicFacade")
//    public AjaxResult uploadCardPicFacade(MultipartFile file){
//        try {
//            String Path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadCardPicFacade";
//            String fileName = UUID.randomUUID().toString() .replaceAll("-","")+"."+ FilenameUtils.getExtension(file.getOriginalFilename());
//            file.transferTo(new File(Path,fileName));
//            return AjaxResult.success("上传成功").put("fileName",fileName);
//        } catch (IOException e) {
//            return AjaxResult.error("上传失败");
//        }
//    }
//    //验证身份证反面
//    @PostMapping("uploadCardPicBack")
//    public AjaxResult uploadCardPicBack(MultipartFile file){
//        try {
//            String Path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadCardPicBack";
//            String fileName = UUID.randomUUID().toString() .replaceAll("-","")+"."+ FilenameUtils.getExtension(file.getOriginalFilename());
//            file.transferTo(new File(Path,fileName));
//            return AjaxResult.success("上传成功").put("fileName",fileName);
//        } catch (IOException e) {
//            return AjaxResult.error("上传失败");
//        }
//    }
//    //验证照片
//    @PostMapping("uploadPic")
//    public AjaxResult uploadPic(MultipartFile file){
//        try {
//            String Path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadPic";
//            String fileName = UUID.randomUUID().toString() .replaceAll("-","")+"."+ FilenameUtils.getExtension(file.getOriginalFilename());
//            file.transferTo(new File(Path,fileName));
//            return AjaxResult.success("上传成功").put("fileName",fileName);
//        } catch (IOException e) {
//            return AjaxResult.error("上传失败");
//        }
//    }
}
